using System;
using System.Collections;
using System.Drawing;
using System.Globalization;
using System.IO;
using MEA.AE_Data.Config;
using MEA.Chart;

namespace MEA.AE_BusinessLogic
{
	public class CSV_Exporter
	{
		public void FileExport2CSV(string fileName, ref ArrayList functs, ConfigData.EGraphicAspect grAsp)
		{
			string text = ";";
			string text2 = "t;";
			bool flag = true;
			int num = 0;
			int count = functs.Count;
			CultureInfo invariantCulture = CultureInfo.InvariantCulture;
			using (StreamWriter streamWriter = new StreamWriter(fileName))
			{
				try
				{
					Function function;
					for (int i = 0; i < count; i++)
					{
						object obj = functs[i];
						if (obj is Function)
						{
							function = (Function)obj;
							string uNIT = function.UNIT;
							uNIT = uNIT.Replace("[", "");
							uNIT = uNIT.Replace("]", "");
							text2 += string.Format(invariantCulture, "ECU_{0}({1})", new object[2] { function.Name, uNIT });
							if (count - 1 > i)
							{
								text2 += text;
							}
						}
						else
						{
							flag = false;
						}
					}
					string text3 = string.Empty;
					string text4 = text2;
					foreach (char c in text4)
					{
						if (c != ' ')
						{
							text3 += c;
						}
					}
					streamWriter.WriteLine(text3);
					if (!flag)
					{
						return;
					}
					if (grAsp == ConfigData.EGraphicAspect.eShowGraphDataLog)
					{
						function = (Function)functs[0];
						num = function.points.Count;
						function = (Function)functs[16];
						if (num > function.points.Count)
						{
							num = function.points.Count;
						}
						num--;
						for (int num2 = num - 1; num2 >= 0; num2--)
						{
							function = (Function)functs[0];
							PointF pointF = function.points[num2];
							text2 = string.Format(invariantCulture, "{0:0.00};", new object[1] { pointF.X });
							for (int k = 0; k < count; k++)
							{
								function = (Function)functs[k];
								pointF = function.points[num2];
								text2 = ((count - 1 <= k) ? (text2 + string.Format(invariantCulture, "{0:0.00}", new object[1] { pointF.Y })) : (text2 + string.Format(invariantCulture, "{0:0.00};", new object[1] { pointF.Y })));
							}
							streamWriter.WriteLine(text2);
						}
						return;
					}
					function = (Function)functs[0];
					num = function.points.Count;
					for (int l = 0; l < num; l++)
					{
						function = (Function)functs[0];
						PointF pointF = function.points[l];
						text2 = string.Format(invariantCulture, "{0:0.00};", new object[1] { pointF.X });
						for (int m = 0; m < count; m++)
						{
							function = (Function)functs[m];
							pointF = function.points[l];
							text2 = ((count - 1 <= m) ? (text2 + string.Format(invariantCulture, "{0:0.00}", new object[1] { pointF.Y })) : (text2 + string.Format(invariantCulture, "{0:0.00};", new object[1] { pointF.Y })));
						}
						streamWriter.WriteLine(text2);
					}
				}
				catch (Exception arg)
				{
					Console.WriteLine("Ex = {0}", arg);
				}
			}
		}
	}
}
